<template>
  <div>
    <el-card>
      <el-form :inline="true" class="demo-form-inline">
          <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-form-item label="用户名"  style="margin-top: 30px">
          <el-input
        
            placeholder="请输入用户"
            v-model="input"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <!-- <el-button type="primary">查询</el-button> -->
          <el-button type="primary" @click="handleAdd"  style="margin-top: 30px">添加</el-button>
        </el-form-item>
      </el-form>
    </el-card>
 <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <el-table :data="tables" style="width: 100%;" border>
      <el-table-column label="Id" width="180" prop="id" type="index">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px;">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品编号" width="180" prop="password">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.password }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
       <el-table-column label="分类名称" width="180" prop="password">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.password }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
       <el-table-column label="密码" width="180" prop="password">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.password }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="密码" width="180" prop="password">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.password }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
 <el-table-column label="是否为管理员">
        <template slot-scope="scope">
          <!-- scope.row  代表一行的数据 scope.row.msg_state 代表状态的值 -->
          <!-- {{scope.row}} -->
          <el-switch
            v-model="scope.row.isManagement"
            @change="manager(scope.$index, scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <!-- scope.row  代表一行的数据 scope.row.msg_state 代表状态的值 -->
          <!-- {{scope.row}} -->
          <el-switch
            v-model="scope.row.isActived"
            @change="onChange(scope.$index, scope.row)"
          ></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="备注" prop="remarks"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 模态框，用于用户的增加和修改 -->
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible">
      <el-form :model="formData">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="formData.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="formData.password" autocomplete="off"></el-input>
        </el-form-item>
        
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="formData.remarks" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分页组件 -->
    <div class="block">
      <el-pagination
        @size-change="handlePageSizeChange"
        @current-change="handlePageIndexChange"
        :current-page="pager.pageIndex"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.rowsTotal"
        background
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { getList, addUser, modUser, deleteUser, vimUser,isAdmin } from '../../api/users'

export default {
  data() {
    return {
      input: '',
      tableData: [],
      pager: {
        pageIndex: 1,
        pageSize: 10,
        rowsTotal: 30,
      },
      dialogFormVisible: false,
      formData: {
        id: 0,
        username: '',
        password: '',
        remarks: '',
        isActived: true,
        isManagement:false,
      
        
      },
      formLabelWidth: '120px',
      currentIndex: 0,
    }
  },
  //监听
  watch: {
    tableData() {
      // val.forEach(item=>{
      //   item.state= Boolean(item.state)
      // })
      // console.log(val);
      //  console.log(this.tableData);
    },
  },
  //挂载
  mounted() {
    this.fetchData(this.pager)
    setTimeout(() => {
      console.log(this.tableData)
    }, 1000)
  },
  //搜索
  computed: {
    tables() {
      const input = this.input
      if (input) {
        console.log('input输入的搜索内容：' + this.input)
        return this.tableData.filter((data) => {
          return data.username.includes(this.input)
        })
      }

      return this.tableData
    },
  },
  //方法
  methods: {
    //用户状态
    onChange(index, row) {
      console.log(index, row)
      this.formData.isActived = row.isActived
      vimUser(row.id, this.formData).then((data) => {
        console.log(data)
        if (data.data.isActived == false) {
          console.log(data.msg);
          this.$message.error(data.msg)
        } else {
          console.log(data.msg);
          this.$message.success(data.msg)
        }
      })
    },
 //是否为管理员
    manager(index, row) {
      console.log(index, row)
      this.formData.isManagement = row.isManagement
      isAdmin(row.id, this.formData).then((data) => {
        console.log(data)
        if (data.data.isManagement == false) {
          console.log(data.msg);
          this.$message.error(data.msg)
        } else {
          console.log(data.msg);
          this.$message.success(data.msg)
        }
      })
    },
    //查询
    handelSelect() {
      this.dialogFormVisible = false

      getList().then((data) => {
        console.log(data)
        this.tableData = data.data.data
      })
    },
    // 用户添加
    handleAdd() {
      this.dialogFormVisible = true
      this.formData.id = 0
      this.formData.username = ''
      this.formData.remarks = ''
      this.formData.password=''
    
    },
    // 用户修改
    handleEdit(index, row) {
      this.dialogFormVisible = true
      this.formData.id = row.id
      this.formData.username = row.username
      this.formData.remarks = row.remarks
      this.formData.password=row.password
 

      // 编辑时获得当前表数组中的下标
      this.currentIndex = index
      console.log(index, row)
    },
    // 删除用户
    handleDelete(index, row) {
      //删除指定id用户
      this.tableData.splice(index, 1)
      //相应删掉id的数据
      deleteUser(row.id)
      //相应的表数据要减掉一条数据
      this.pager.rowsTotal = this.pager.rowsTotal - 1
      this.$message('删除成功')
      console.log(index, row)
      console.log(index, row)
    },
    // （无论是添加还是修改后）保存用户信息
    handleSave() {
      this.dialogFormVisible = false
      console.log(this.formData)
      // 如果表单中的id为0，则认为是添加用户，反之则为修改用户
      if (this.formData.id === 0) {
        addUser(this.formData)
          .then((data) => {
            console.log(data)
            //将新添加的数据push到现有表数据的最后面
            if (data.code == 400) {
              this.$message(data.msg)
            } else {
              this.tableData.push(data.data)
              this.$message(data.msg)
              this.fetchData(this.pager)
            }
          })
          .catch((err) => {
            console.log(err)
          })
      } else {
        modUser(this.formData.id, this.formData)
          .then((data) => {
            console.log(data)
            //修改完重新拉取数据
            this.tableData.splice(this.currentIndex, 1, data.data)
            // this.fetchData(this.pager)
            this.$message('编辑成功')
          })
          .catch((err) => {
            console.log(err)
          })
      }
    },
    handleCancel() {
      this.dialogFormVisible = false
    },
    // 页面页码改变时，重新拉取数据
    handlePageIndexChange(val) {
      console.log(val)
      this.pager.pageIndex = val > 0 ? val : 1
      this.fetchData(this.pager)
    },
    // 页面大小改变时，重新拉取数据
    handlePageSizeChange(val) {
      console.log(val)
      this.pager.pageSize = val
      this.fetchData(this.pager)
    },
    // 拉取后台数据（带分页）
    fetchData(pager) {
      getList(pager).then((data) => {
        data.data.data.forEach((item) => {
          item.state = item.isActived ? 1 : 0
        })
        this.tableData = data.data.data
        this.pager = data.data.pager
      })
    },
  },
}
</script>
